// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import VueResource  from 'vue-resource'
import stores from './stores'
import router from './router'
import App from './app.vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'
//import 'normalize.css'
import './iconfont/iconfont.css'
import './assets/css/bootstrap/css/bootstrap.min.css'
import './filter'
import  './less/common.less'
import  components from './components'
import  VueValidator from 'vue-validator'
import _ from 'lodash'
import VueHighlightJS from 'vue-highlightjs'

// Tell Vue.js to use vue-highlightjs
Vue.use(VueHighlightJS);
//Vue.config.productionTip = false
Vue.use(ElementUI);
Vue.use(VueValidator);
Vue.use(VueResource);
//拦截器
Vue.http.options.emulateJSON = true;
// let text = "正在加载...";
// const showLoading = stores.state.loading.showLoading;
// Vue.http.interceptors.request.use(function (config) {
//   if(stores.state.loading.text !== ''){
//     text = stores.state.loading.text;
//   }
//   //全局加载提示
//   if(showLoading){
//     var loadingInstance = ElementUI.Loading.service({
//       body: false,
//       fullscreen: true,
//       text:text,
//       lock: false,
//       customClass:'ui-loading'
//     });
//   }
// });
// Vue.http.interceptors.response.use(function (response) {
//   //从vuex取出状态文本
//   const statusText = stores.state.responseStatus[response.status];
//   //关闭加载
//   if(showLoading){
//     loadingInstance.close();
//   }
//   //重置提示文本
//   text = "正在加载...";
//   stores.dispatch('setLoadingText','');
//   stores.dispatch('setShowLoading',true);
//   response.statusText = typeof(statusText) !== 'undefined' ? statusText : response.statusText;
//   switch (response.status){
//     case 401 :
//       router.replace("/login")
//       break;
//     default:
//       break;
//   }
// });
Vue.http.interceptors.push( function(request, next)  {
    let text = "正在加载...";
    const showLoading = stores.state.loading.showLoading;
    //if(request.method.toUpperCase() == 'POST') {
    //  text = '正在提交...';
    //}
    //如果vuex中设置了加载提示文本
    if(stores.state.loading.text !== ''){
        text = stores.state.loading.text;
    }
    //全局加载提示
    if(showLoading){
        var loadingInstance = ElementUI.Loading.service({
            body: false,
            fullscreen: true,
            text:text,
            lock: false,
            customClass:'ui-loading'
        });
    }

    // setTimeout(function () {//
        next(function(response) {

            //从vuex取出状态文本
            const statusText = stores.state.responseStatus[response.status];
            //关闭加载
            if(showLoading){
                loadingInstance.close();
            }
            //重置提示文本
            text = "正在加载...";
            stores.dispatch('setLoadingText','');
            stores.dispatch('setShowLoading',true);
            response.statusText = typeof(statusText) !== 'undefined' ? statusText : response.statusText;
            switch (response.status){
                case 401 :
                    router.replace("/login")
                    break;
                default:
                break;
            }

        })
    // },500);
});


Object.keys(components).forEach((key) => {
    //注册公共组件，
    Vue.component(`v${key}`, components[key]);
});

// router.beforeEach((to,from,next) => {
//   let {auth = true } = to.meta;
//   const isLogin = typeof(stores.state.User.userId) !== 'undefined';
//   if(isLogin){
//     if(to.path === '/login'){
//       return next({path:'/'});
//     }else{
//       //判断权限菜单是否有数据
//       if(stores.getters.getElements.length === 0){
//         stores.dispatch('getElements').then(res => {
//           if(res.elements){
//             if(hasPermission(to.meta.code)){
//               return next({path:'/authorization'});
//             }else{
//               next();
//             }
//           }else{
//             stores.dispatch('logout');
//             return next({path:'/login'});
//           }
//         }).catch(err => {
//           stores.dispatch('logout');
//           return next({path:'/login'});
//           //console.log(err);
//         });
//       }else{
//         //权限验证
//         if(hasPermission(to.meta.code)){
//           return next({path:'/authorization'});
//         }else{
//           next();
//         }
//       }
//     }
//   }else{
//     //免登陆页面
//     if(!auth){
//       next();
//     }else{
//       return next({path:'/login'});
//     }
//   }
// });


//面包屑导航值保存到vuex中
router.afterEach( router => {
    stores.commit('setBreadCrumbs',router.matched);
});

/* eslint-disable no-new */
new Vue({
    store: stores,
    router,
    render: h => h(App)
}).$mount('#app');




